/**
 * 
 */
package edu.arizona.bio5.onto.decomposition;

import java.net.URI;

import org.semanticweb.owlapi.apibinding.OWLManager;
import org.semanticweb.owlapi.model.IRI;
import org.semanticweb.owlapi.model.OWLOntology;
import org.semanticweb.owlapi.model.OWLOntologyManager;
import org.semanticweb.owlapi.util.OWLOntologyImportsClosureSetProvider;
import org.semanticweb.owlapi.util.OWLOntologyMerger;

import uk.ac.manchester.cs.owlapi.modularity.ModuleType;
import edu.arizona.bio5.onto.util.OntologyUtils;
import edu.arizona.bio5.timers.StaticTimers;

/**
 * Used to serialize and examine a single AD
 * 
 * @author Pavel Klinov
 *
 */
public class SingleADExaminer {

	//static final String ONTO_URI = "file:eval/GO/GO_1K.owl";
	static final String ONTO_URI = "file:/Users/pklinov/work/UA/j2ee/modularity_experiments/eval/data/GO/GO.owl.xml";
	//static final String AD_FOLDER = "eval/GO/go_1K_bottom_xml";
	
	static OWLOntologyManager m_mgr = OWLManager.createOWLOntologyManager();
	
	private static OWLOntology loadOntology(String uri) throws Exception {
		
		OWLOntology ontology = m_mgr.loadOntologyFromOntologyDocument( IRI.create(URI.create(uri)) );
		OWLOntologyImportsClosureSetProvider provider = new OWLOntologyImportsClosureSetProvider(m_mgr, ontology);
		OWLOntologyMerger merger = new OWLOntologyMerger(provider);
		OWLOntology closure = merger.createMergedOntology(m_mgr, IRI.create(ontology.getOntologyID().getOntologyIRI().toString() + "/importclosure"));
		
		OntologyUtils.prepareForSyntacticModularity(closure);
		
		System.out.println(closure.getLogicalAxiomCount() + " axioms to be partitioned");
		
		return closure;
	}
	
	
	/**
	 * @param args
	 */
	public static void main(String[] args) throws Exception {

		/*AtomicDecomposition pad = (AtomicDecomposition)new DiskADLoader().load(new File("/Users/pklinov/work/UA/java/modularity_experiments/eval/CHEBI/chebi_bot_ad"));
		
		System.out.println(pad.stats());*/
		
		OWLOntology ontology = loadOntology(ONTO_URI);
		ADAlgorithm algo = new DmitryDecompositionAlgorithm(ModuleType.BOT); 
		
		StaticTimers.start("ad");
		
		AtomicDecomposition ad = algo.decompose( m_mgr, ontology );
		
		StaticTimers.stop("ad");
		
		//System.out.println(ad.stats());
		StaticTimers.printAll();
		
		AtomLabelingAlgorithm labelAlgo = new MSSLabelingAlgorithm();
		
		labelAlgo.compute(ad);
		//Serialize the LAD
		//XMLDBPersister persister = new XMLDBPersister("xmldb:exist://localhost:8080/exist/xmlrpc/db", "SWEET", "admin", "");
		
		//persister.setRootDir(new File(AD_FOLDER));
		//persister.persist(ad);		
	}
}